package trinidad;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class BestResults {
	public static final int NUMBER_OF_RESULTS = 10;
	private ArrayList<Result> resultsList;
	private final String filePath = "results";
	
	public BestResults() {
		resultsList = new ArrayList<Result>();
		readResultsFile();
	}
	
	private void sort()
	{
		Result[] array = toArray(resultsList);
		Result v;
		int j = 0;
		for (int i = 1 ; i < array.length ; i++)
		{
			j = i;
			v = array[i];
			while ( (j > 0) &&(array[j - 1].getResult() > v.getResult()) )
			{
				array[j] = array[j-1];
				j--;
			}
			array[j] = v;
		}
		resultsList.clear();
		resultsList = toArrayList(array);
	}
	
	private ArrayList<Result> toArrayList(Result[] array) {
		ArrayList<Result> list = new ArrayList<Result>();
		for(int i = 0 ; i < array.length ; i ++) {
			list.add(array[i]);
		}
		return list;
	}
	
	private Result[] toArray(ArrayList<Result> list) {
		Result[] res = new Result[list.size()];
		for (int i = 0 ; i < list.size() ; i++) {
			res[i] = list.get(i); 
		}
		return res;
	}
	
	public void addResult(Result result) {
		resultsList.add(result);
		sort();
		while (resultsList.size() > NUMBER_OF_RESULTS) {
			resultsList.remove(resultsList.size() - 1);
		}
		writeResultsFile();
	}
	
	public void addResult(int result, String name) {
		resultsList.add(new Result(result,name));
		sort();
		while (resultsList.size() > NUMBER_OF_RESULTS) {
			resultsList.remove(resultsList.size() - 1);
		}
		writeResultsFile();
	}
	
	public int getWorstResult() {
		if (resultsList.size() > 0) {
			return resultsList.get(resultsList.size() - 1).getResult();
		}
		return Integer.MAX_VALUE;
	}
	
	public int getResultsNumber() {
		return resultsList.size();
	}
	
	public String toString() {
		String result = "";
		
		for (int i = 0; i < resultsList.size() ; i++) {
			result += (i+1) + ". \"" +  resultsList.get(i).getName() + "\" - ruchy: " + resultsList.get(i).getResult() +"\n";
		}
		return result;
	}
	
	
	private void readResultsFile() {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(new File(filePath)));
			String line = "";
			StringTokenizer tokenizer = null;
			
			while ((line = reader.readLine()) != null) {
				int result = 0;
				String name = "";
				tokenizer = new StringTokenizer(line, ",");
				
				if (tokenizer.hasMoreTokens()) {
					result = Integer.parseInt(tokenizer.nextToken());
				}
				while (tokenizer.hasMoreTokens()) {
					name += tokenizer.nextToken();
					if(tokenizer.hasMoreTokens()) {
						name += ",";
					}
				}
				resultsList.add(new Result(result,name));
			}
			reader.close();
		} catch (IOException e) {
			System.out.println("LOL");
		}
	}
	
	private void writeResultsFile() {
		try {
			PrintWriter printWriter = new PrintWriter(new FileWriter(new File(filePath)));
			for (Integer i = 0 ; i < resultsList.size() ; i++) {
				printWriter.println(resultsList.get(i).getResult() + "," + resultsList.get(i).getName());
			}
			printWriter.close();
		} catch (IOException e) {
			System.out.println("I JUH");
		}
	}
}
